package org.aplikator.client.widgets;

import org.aplikator.client.Aplikator.AplikatorResources;
import org.aplikator.client.descriptor.PropertyDTO;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.TextArea;

public class TextAreaWidget extends DataFieldBase<String> implements DataField<String> {
    private TextArea area = new TextArea();

    public TextAreaWidget(String caption, PropertyDTO<String> property, boolean labelAbove, String width, String height) {
        super(caption, property, false, labelAbove);
        add(area);
        area.addStyleName(PaneWidgetResources.INSTANCE.css().formData());
        area.addValueChangeHandler(new ValueChangeHandler<String>() {
            public void onValueChange(ValueChangeEvent<String> event) {
                setDirty(true);
            }
        });
        if (property.getRefferedThrough() != null) {
            this.setEnabled(false);
        }
        if (width != null) {
            area.setWidth(width);
        } else {
            area.setWidth("50em");
        }

        if (height != null) {
            area.setHeight(height);
        } else {
            area.setHeight("4em");
        }
    }

    public void setValue(String value) {
        area.setValue(value);
    }

    public String getValue() {
        return new String(area.getValue());
    }

     public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
        return area.addValueChangeHandler(handler);
    }

    public void setEnabled(boolean enabled) {
        area.setEnabled(enabled);
    }

    public void setDirty(boolean dirty) {
        super.setDirty(dirty);
        if (dirty) {
            area.addStyleName(AplikatorResources.INSTANCE.css().dirty());
        } else {
            area.removeStyleName(AplikatorResources.INSTANCE.css().dirty());
        }
    }

}
